using System.Collections;
using System.Collections.Generic;
using EventUtils;
using UnityEngine.UI;
using EventType = UnityEngine.EventType;

namespace View.Instant
{
    public class DepotPowerView : View
    {
        private class ItemInfo
        {
            public Text _txtAdd;
            public Text _txtNeed;
            public Button _btn;
        }

        private Button mbtnClose;
        private ItemInfo[] mItemList = new ItemInfo[2];
        private Text mtxtCurPower;
        private Text mtxtDaojishi;
        private Button mbtnGet;
        private Text mtxtTipsUpdatePowerTimer;
        public override void OnLoad()
        {
            base.OnLoad();
            
            mbtnClose = transform.Find("btnClose").GetComponent<Button>();
            for (int i = 0; i < mItemList.Length; i++)
            {
                mItemList[i] = new ItemInfo();
                mItemList[i]._txtAdd = transform.Find((i + 1) + "/add").GetComponent<Text>();
                mItemList[i]._txtNeed = transform.Find((i + 1) + "/dengdai/needpay").GetComponent<Text>();
                mItemList[i]._btn = transform.Find((i + 1) + "/dengdai").GetComponent<Button>();
            }
            mtxtCurPower = transform.Find("0/txttili").GetComponent<Text>();
            mtxtDaojishi = transform.Find("0/dengdai/timer").GetComponent<Text>();
            mbtnGet = transform.Find("0/dengdai").GetComponent<Button>();
            mtxtTipsUpdatePowerTimer = transform.Find("0/tishi").GetComponent<Text>();
            mtxtTipsUpdatePowerTimer.text = string.Format("每{0}分钟恢复一点", GameLocalInfo.mGetDepotPowerNeedTime / 60);

            AddClickListener(mbtnClose,() =>
            {
                UIManager.Instance.Close(ViewName.DepotPowerView);
            });

            for (int i = 0; i < mItemList.Length; i++)
            {
                int index = i;
                mItemList[i]._txtAdd.text = "+1";
                if (i==0)
                {
                    mItemList[i]._txtNeed.text = "100";
                }
                
                AddClickListener(mItemList[i]._btn, () =>
                {
                    if (index == 0)
                    {
                        //购买
                        if (GameLocalInfo.mPlayerGold >= 100)
                        {
                            GameLocalInfo.OnUpdatePlayerGold(-100);
                            GameHelp.OnAddSelfPlayerPropItem(GameLocalInfo.OnGetPropItemTableCloneById(PropItemEnum.tili));
                        }
                        else
                        {
                            UIManager.Instance.Show(ViewName.ToolTipsView, ViewLayer.Top, "金币不足！");
                            //提示钱不够
                        }
                    }
                    else if (index == 1)
                    {
                        //看视频
                        UIManager.Instance.OnShowLoading();
                        AndroidManager.Ins.LoadReward(() =>
                        {
                            UIManager.Instance.OnCloseLoading();
                            TaskManager.OnAddTaskNumByTaskType(TaskTypeEnum.adv);
                            EventCenter.BroadCast<string>(EventUtils.EventType.EVENT_SEE_ADV_END,ViewName.DepotPowerView);
                        });
                    }
        
                });
                // AddClickListener(mbtnGet, () =>
                // {
                //     //去看广告   回来之后获得这些
                //     string timer = GameLocalInfo.OnGetTimeForDepotPower();
                //     if (timer == "-1")
                //     {
                //         // 直接领取
                //         GameLocalInfo.mLoginForDepotPowerTimer = GameLocalInfo.OnGetNowTimeToDepotPower();
                //         GameHelp.OnAddSelfPlayerPropItem(GameLocalInfo.OnGetPropItemTableCloneById(PropItemEnum.tili));
                //         EventCenter.BroadCast<List<PropItemEnum>>(EventUtils.EventType.EVENT_GONGXIHUODE,new List<PropItemEnum>(){PropItemEnum.tili});
                //     }
                //     else
                //     {
                //         AdvCtrlManager.LoadReward(() =>
                //         {
                //             EventCenter.BroadCast<string>(EventUtils.EventType.EVENT_SEE_ADV_END,ViewName.DepotPowerView);
                //         });
                //     }
                // });
            }
        }

        private void Update(float trimer)
        {
            string timer = GameLocalInfo.OnDepotPowerTimeSub();
            if (timer == "-2")
            {
                mtxtDaojishi.text = "体力已满";
            }
            else
            {
                mtxtDaojishi.text = timer;
            }
        }
        
        private void OnSeeAdvCallback(string str)
        {
            if (str == ViewName.DepotPowerView)
            {
                GameHelp.OnAddSelfPlayerPropItem(GameLocalInfo.OnGetPropItemTableCloneById(PropItemEnum.tili));
                EventCenter.BroadCast<List<PropItemEnum>>(EventUtils.EventType.EVENT_GONGXIHUODE,new List<PropItemEnum>(){PropItemEnum.tili});
            }
        }

        public override void OnShow(params object[] args)
        {
            base.OnShow();
            Utils.OnUpdate += Update;
            mtxtCurPower.text = string.Format("当前体力 {0}", GameLocalInfo.mPlayerPower);
            EventCenter.AddListener<string>(EventUtils.EventType.EVENT_SEE_ADV_END,OnSeeAdvCallback);
        }

        public override void OnClose(params object[] args)
        {
            base.OnClose();
            Utils.OnUpdate -= Update;
            if (gameObject.activeSelf)
            {
                EventCenter.RemoveListener<string>(EventUtils.EventType.EVENT_SEE_ADV_END,OnSeeAdvCallback);
            }
        }
    }

}
